<%@ page pageEncoding="UTF-8"%>
<%@ include file="../commons/head.jsp"%>
<title>管理员账号管理</title>
<style type="text/css">
</style>
<script type="text/javascript">
	var isValid;
	var userRole;
	var data;
	var flag = true;
	var reloadFlag;
	//init();
	$.extend($.fn.validatebox.defaults.rules, {
		//密码验证输入
	    pass: {
	        validator: function (value,param) {
	        	if(!/[a-zA-Z0-9]+/.test(value)){
	        		$.fn.validatebox.defaults.rules.pass.message= "只能输入字母和数字";
	        		return false;
	        	} else { 
	        		if(value.length < param[0] || value.length > param[1]){
	        			$.fn.validatebox.defaults.rules.pass.message= '密码长度必须在' + param[0] + '至' + param[1] + '范围';
	        			return false;
	        		}else{
	            		return true;
	        		}
	        	}
	        	
	        },
	    },
	  	//重复密码验证输入
	    repeatpass: {
	        validator: function (value,param) {
	        	var password = $("#password").val();
	        	if(value!=password){
	        		$.fn.validatebox.defaults.rules.repeatpass.message= "重复密码必须与密码一致";
	        		return false;
	        	}else{
	        		return true;
	        	}
	        },
	    },
	   
	  	//用户ID验证输入
	    acc: {
	        validator: function (value,param) {
        		if(value.length < param[0] || value.length > param[1]) {
        			$.fn.validatebox.defaults.rules.acc.message = '用户账户长度必须在' + param[0] + '至' + param[1] + '范围';
        			return false;
        		} else {
            		return true;
        		}
	        },
	    },
	    
	});

	$(function(){
		
		$("#search").click(function(){
			var params = {
				userAccount:$("#user_account").val(),
				userRole:$("#user_role").val()
			};
			$("#tt").datagrid('load', params);
		});
		
		$("#add").click(function(){
			reloadFlag = true;
			$("#fm").form('clear');
			$("#dlg").dialog("open");
			$("#userAccount").textbox("enable");
			$(".panel-title").text('新增');
			$("#userRole").val(1);
			$("#password").textbox("enableValidation");
		});
		//编辑用户信息
		$("#edit").click(function(){
			var row = $('#tt').datagrid('getSelected');
			if(!row){
				$.messager.alert("请选择一行","请选择一行","error");
				return ;
			}
			
			var id = row.id;
			var role = row.userRole;
			reloadFlag = false;
			$("#fm").form('load','${ctx}/tusersetting/edit?id='+id);
			$("#dlg").dialog("open");
			$(".panel-title").text('编辑');
		});	

		
		$('#fm').form({
	    	url:'${ctx}/tusersetting/save',
	    	onSubmit: function(){
	    		var id = $("#id").val();
	    		if( id != null && id != ""){
	    		} else {
	    			return $(this).form('validate');
	    		}
	   		},
	    	success:function(data){
		    	data = eval("("+data+")");
				if(data.result==1){
						$.messager.alert("成功","成功","info",reload);
				}else{
					$.messager.alert("失败：",data.msg,"error");
				}
	    	}
		});
		//init();
		
		
		$("#delete").click(function(){
			var row = $('#tt').datagrid('getSelected');
			if(!row){
				$.messager.alert("请选择一行","请选择一行","error");
				return ;
			}
			var id = row.id;
			$.messager.confirm("你确定要删除吗","你确定要删除吗",function(r){
			if(r){
			

			$.ajax({
				url:'${ctx}/tusersetting/delete',
				contentType: "application/json; charset=utf-8",
				method:'delete',
				data:JSON.stringify(row),
				dataType:'json',
				success:function(data){
					if(data.result==1){
						$.messager.alert("成功","成功","info",reload);
					}else{
						$.messager.alert("失败",data.msg,"error");
					}
				}
			});
			}
			});

	 
		});
	});

	
	function saved(){
		$('#fm').submit();
	};

	function reload(closeFlag){
		if(reloadFlag){
			$("#fm").form('clear');
		}
		if(closeFlag){
			$("#dlg").dialog('close');
		}
	$("#tt").datagrid('reload');
	}

	function closeDlg(){
		$("#tt").datagrid('reload');
		$('#dlg').dialog('close');
	};

	function rowStyle(index,row){
		if (index%2==0){     
	        return 'background-color:lightcyan;';     
	    }     
	}
	//鼠标悬停显示提示
	function showTips(value, row, index) {
		var content = '';
		if(value != null && value != ''){
		    content = '<span title="' + value + '" class="note" >' + value + '</span>';
		}
	    return content;
	}
</script>
</head>
<body>
<div class="jianju">
<h3>管理员账号管理</h3>
<div id="tb" style="height:auto">
<div>
	<a id="add" href="#" class="easyui-linkbutton button" iconCls="icon-add" plain="false">新增</a>
	<a id="edit" href="#" class="easyui-linkbutton button" iconCls="icon-edit" plain="false">编辑</a>
	<a id="delete" href="#" class="easyui-linkbutton button" iconCls="icon-remove" plain="false">删除</a>
</div>
<hr>
<div>
	<input type="hidden" id = "user_role" value="1">
	<label style="margin-left:15px;">用户账号：</label><input id="user_account" class="easyui-textbox" style="width:100px">
	<a id="search" href="#" class="easyui-linkbutton button" style="margin-left:15px;" iconCls="icon-search">查询</a>	
</div>
</div>

	<table id="tt" class="easyui-datagrid" 
		url="${ctx}/tusersetting/list?userRole=1"
		title="" style="width:98%;minWidth:1000px;"  iconCls="icon-save"
		rownumbers="true" pagination="true"
		pageList="[10,20,30,40]" method="get"
		toolbar="#tb" singleSelect="true"
		fitColumns="true" idField="id" rowStyler="rowStyle"
		onLoadSuccess:function(data){
                $(".note").tooltip({
                    onShow: function(){
                        $(this).tooltip('tip').css({ 
                            width:'300',
                            boxShadow: '1px 1px 3px #292929'       
                                             
                        });
                    }
                });
             } >
		<thead>
			<tr>
				<th data-options="field:'userAccount',formatter:showTips" style="width:49% ">用户账号</th>
				<th data-options="field:'password',formatter:showTips"  style="width:50% ">密码</th>
			</tr>
		</thead>
	</table>
</div>

	<div id="dlg" class="easyui-dialog" style="width:500px;padding:10px 20px"
		closed="true" buttons="#dlg-buttons" modal="true">
		<div class="ftitle"></div>
		<form id="fm" method="post" class="form-horizontal" role="form">
			<input type="hidden" id = "id" name="id">
			<input type="hidden" id = "userRole" name="userRole" value="1">
			<div class="fitem form-group">
				<label class="col-sm-3 control-label">账号：</label>
				<div class="col-sm-4">
					<input name="userAccount" id="userAccount" class="easyui-validatebox easyui-textbox" style="width:200px" required validType="acc[5,20]">
				</div>
			</div>
			
			<div class="fitem form-group"> 	
				<label class="col-sm-3 control-label">密码：</label>
				<div class="col-sm-4">
						<input name="password" id="password" type="text" required="false" class="easyui-validatebox easyui-textbox"  style="width:200px" validType="pass[6,20]"/>
				</div>
			</div>
			
			<div class="fitem form-group"> 	
				<label class="col-sm-3 control-label">重复密码：</label>
				<div class="col-sm-4">
						<input name="repeatpass" id="repeatpass" type="text" required="false" class="easyui-validatebox easyui-textbox"  style="width:200px" validType="repeatpass[6,20]"/>
				</div>
			</div>
			
		</form>
	</div>
	
	<div id="dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="javascript:saved()">保存</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:reload(true);">取消</a>
	</div>


</body>
</html>